package com.d3works.apps.production.web;

import com.d3works.apps.production.db.BillOfMaterial;
import com.d3works.apps.production.exceptions.ProductionExceptionMessage;
import com.d3works.apps.production.service.BillOfMaterialService;
import com.d3works.apps.sale.db.SaleOrderLine;
import com.d3works.exception.service.TraceBackService;
import com.d3works.i18n.I18n;
import com.d3works.inject.Beans;
import com.d3works.rpc.ActionRequest;
import com.d3works.rpc.ActionResponse;
import com.google.inject.Singleton;

@Singleton
public class SaleOrderLineController {

  public void customizeBillOfMaterial(ActionRequest request, ActionResponse response) {
    try {
      SaleOrderLine saleOrderLine = request.getContext().asType(SaleOrderLine.class);

      BillOfMaterial copyBillOfMaterial =
          Beans.get(BillOfMaterialService.class).customizeBillOfMaterial(saleOrderLine);

      if (copyBillOfMaterial != null) {

        response.setValue("billOfMaterial", copyBillOfMaterial);
        response.setInfo(I18n.get(ProductionExceptionMessage.SALE_ORDER_LINE_1));
      }
    } catch (Exception e) {
      TraceBackService.trace(response, e);
    }
  }
}
